﻿package me.zhengjie.common.config

import me.zhengjie.common.utils.SecurityUtils
import org.springframework.security.core.GrantedAuthority
import org.springframework.stereotype.Service
import java.util.*
import java.util.stream.Collectors


@Service(value = "el")
class AuthorityConfig {
    fun check(vararg permissions: String): Boolean {
        // 如果是匿名访问的，就放行
        val anonymous = "anonymous"
        if (Arrays.asList(*permissions).contains(anonymous)) {
            return true
        }
        // 获取当前用户的所有权限
        val elPermissions: List<String> =
            SecurityUtils.userDetails.getAuthorities().stream().map { obj: GrantedAuthority -> obj.authority }
                .collect(Collectors.toList())
        // 判断当前用户的所有权限是否包含接口上定义的权限
        val list = Arrays.stream(permissions).filter { o: String ->
            elPermissions.contains(
                o
            )
        }.collect(Collectors.toList())
        // return elPermissions.contains("admin") || list.size() != 0;
        return elPermissions.contains("admin") || list.size != 0
    }
}
